#!/usr/bin/env groovy

// import java.util.List as jurist
/*
ver:
*/
def mute(...ps) {
}

def vari() {
    println("常量变量")
    int a = 10
    def b = "b" 
    b="1"

    b.toInteger()

    mute(a,b,a.class)
}

def stri() {
    println("字符串")
    x=0
    def a= 'abc'
    def b= "i am $a"
    def c= "${2+3}"
    def lines= '''
        a
        b
    '''.trim()

    def str = "foo"
    def pattern = ~/fo*/
    assert str =~ pattern
    str?.toUpperCase() //防止空指针

    assert "1234"[1..3]=="234"

    mute(a,b,lines)
}

def coll() {
    println("数组集合")

    def arr = []
    def map = [:]
    def colors = [red: '#FF0000', green: '#00FF00', blue: '#0000FF']
    def range = 0..5

    arr.add(1)
    arr.add(2)
    arr.add(3)
    arr.add(4)
    arr.remove(3)
    assert arr[1..2] == [2,3]
    map.put('a',1)
    map.b == 2
    colors['red']


    assert 1 in arr 
    assert ["JAVA", "GROOVY"] == ["Java", "Groovy"]*.toUpperCase()

    arr.each{mute(it)}
    assert arr.collect { it * 2 } == [2, 4, 6]
    map.each{k,v->
        mute(k,v)
    }

    sum = 0
    for (i in [0, 1, 2]) {
        sum += i
    }

    for (i in ['java': 1, 'groovy': 2, 'python': 3]) {
        mute("key:${i.key} value:${i.value}")
    }
}

def proc() {
    println("流程控制")

    str = 'str'
    if (str.contains("hello")) {
        str=str - "hello"
    } else {
        str=str+"..."
    }

    def sum = 0
    while(sum<10){
        sum++
    }

    x=0.0
    switch (x) {
        case "foo": result = "found foo"
            // lets fall through
        case "bar": result += "bar"
        case [4, 5, 6, 'inList']: result = "list"
            break
        case 12..30: result = "range"
            break
        case Integer: result = "integer"
            break
        case Number: result = "number"
            break
        case { it > 3 }: result = "number > 3"
            break
        default: result = "default"
    }
    assert result == "number"
}

def func() {
    println("函数方法")
    def f1= {a,b->a+b}

    f1(1,2)
}
def clas() {
    println("类和对象")


    def a = new A(a:'a')
    def p = new Person()
    p.Named__name = 'p'

    mute(a.a)
    mute(p)
}

def static main(args) {
    println("Hello, World! Groovy 兼容 Java")
    vari()
    stri()
    coll()
    proc()
    func()
    clas()
}

trait Named {
    public String name
}
class Person implements Named {}

class A{
    def a 
    class B {
        def b
    }
}